<template>
  <div class="editpass">
    <el-form :rules="rules" ref="ruleForm" label-width="80px" :model="form">
      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="旧密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="form.newpassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPassOk">
        <el-input v-model="form.newPassOk"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit(form)" type="primary">确定修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { request } from "@/api/request";
export default {
  data() {
    return {
      // 表单数据
      form: {
        username: JSON.parse(window.localStorage.getItem("userInfo")).username,
        id: JSON.parse(window.localStorage.getItem("userInfo")).id,
      },
      // 表单验证
      rules: {
        password: [
          { required: "true", message: "请输入旧密码", trigger: "blur" },
        ],
        newpassword: [
          { required: "true", message: "请输入新密码", trigger: "blur" },
        ],
        newPassOk: [
          { required: "true", message: "请确认新密码", trigger: "blur" },
        ],
      },
      // 防抖计时器
      timer: null,
    };
  },
  methods: {
    async submit(e) {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
          this.editpass(e)
      }, 500);
    },
    // 提交表单修改密码
    editpass(e) {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return this.$message.info("表单数据不完整！");
        if (this.form.newpassword !== this.form.newPassOk)
          return this.$message.info("两次输入的新密码不正确");
        request({
          params: e,
          route: "editpass",
          url: "user",
          method: "put",
        }).then((res) => {
          console.log(res);
        });
      });
    },
  },
};
</script>
<style lang="less" scoped>
.el-input {
  width: 30%;
}
</style>